<script>
var merge = function(intervals) {
  intervals.sort((a,b)=>a[0]-b[0]);
  for(let i = 1;i<intervals.length;i++){
    const prev = intervals[i-1];
    const now = intervals[i];
    if(now[0]<=prev[1]){
      intervals[i-1][1] = Math.max(now[1],prev[1]);
      intervals.splice(i,1);
      i--;
    }
  }
  return intervals;
};
console.log(merge([[1,3],[2,6],[8,10],[15,18]]));
</script>
